//
//  MarsAuthorizeManager.m
//  MarsWebDemo
//
//  Created by yangpenghua on 2018/3/28.
//  Copyright © 2018年 linzi. All rights reserved.
//

#import "MarsAuthorizeManager.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "UIViewController+MarsVisible.h"

@implementation MarsAuthorizeManager

//  通讯录打开授权
+ (void)mars_authorizeAddressBook{
    NSDictionary *infoDict = [NSBundle mainBundle].localizedInfoDictionary;
    if (!infoDict) {
        infoDict = [NSBundle mainBundle].infoDictionary;
    }
    NSString *appName            = [infoDict objectForKey:@"CFBundleDisplayName"];
    if (appName == nil) {
        appName = @"";
    }
    NSString *alertMessage       = [NSString stringWithFormat:@"请允许%@访问手机通讯录", appName];
    NSString *title              = [NSString stringWithFormat:@"温馨提示"];
    
    [self authorizeAlertWithTitle:title message:alertMessage];
}

//  地理位置打开授权
+ (void)mars_authorizeGeographicLocation{
    NSDictionary *infoDict = [NSBundle mainBundle].localizedInfoDictionary;
    if (!infoDict) {
        infoDict = [NSBundle mainBundle].infoDictionary;
    }
    NSString *appName            = [infoDict objectForKey:@"CFBundleDisplayName"];
    if (appName == nil) {
        appName = @"";
    }
    NSString *alertMessage       = [NSString stringWithFormat:@"请允许%@访问地理位置", appName];
    NSString *title              = [NSString stringWithFormat:@"温馨提示"];
    
    [self authorizeAlertWithTitle:title message:alertMessage];
}

//  相册打开授权
+ (void)mars_authorizePhotoAlbum{
    NSDictionary *infoDict = [NSBundle mainBundle].localizedInfoDictionary;
    if (!infoDict) {
        infoDict = [NSBundle mainBundle].infoDictionary;
    }
    NSString *appName            = [infoDict objectForKey:@"CFBundleDisplayName"];
    if (appName == nil) {
        appName = @"";
    }
    NSString *alertMessage       = [NSString stringWithFormat:@"请允许%@访问手机相册", appName];
    NSString *title              = [NSString stringWithFormat:@"温馨提示"];
    
    [self authorizeAlertWithTitle:title message:alertMessage];
}

//  相机打开授权
+ (void)mars_authorizeCamera{
    NSDictionary *infoDict = [NSBundle mainBundle].localizedInfoDictionary;
    if (!infoDict) {
        infoDict = [NSBundle mainBundle].infoDictionary;
    }
    NSString *appName            = [infoDict objectForKey:@"CFBundleDisplayName"];
    if (appName == nil) {
        appName = @"";
    }
    NSString *alertMessage       = [NSString stringWithFormat:@"请允许%@访问手机相机", appName];
    NSString *title              = [NSString stringWithFormat:@"温馨提示"];
    
    //  iOS 判断应用是否有使用相机的权限
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        [self authorizeAlertWithTitle:title message:alertMessage];
    }
}

//  麦克风打开授权
+ (void)mars_authorizeMicrophone{
    NSDictionary *infoDict = [NSBundle mainBundle].localizedInfoDictionary;
    if (!infoDict) {
        infoDict = [NSBundle mainBundle].infoDictionary;
    }
    NSString *appName            = [infoDict objectForKey:@"CFBundleDisplayName"];
    if (appName == nil) {
        appName = @"";
    }

    NSString *alertMessage       = [NSString stringWithFormat:@"请允许%@访问手机麦克风", appName];
    NSString *title              = [NSString stringWithFormat:@"温馨提示"];
    
    //  iOS 判断应用是否有使用麦克风的权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//读取设备授权状态
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        [self authorizeAlertWithTitle:title message:alertMessage];
    }
}

//  权限打开弹窗
+ (void)authorizeAlertWithTitle:(NSString *)title message:(NSString *)message{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction  = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *setupAction   = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:setupAction];
    
    UIViewController *currentVC = [UIViewController mars_currentViewController];
    [currentVC presentViewController:alertController animated:YES completion:nil];
}

@end
